package stateAAs
{
	import flash.events.Event;
	import flash.system.System;
	import flash.text.TextField;
	
	import org.agony2d.Agony;
	import org.agony2d.gpu.FusionAA;
	import org.agony2d.gpu.ImageAA;
	import org.agony2d.gpu.StateAA;
	import org.agony2d.gpu.TintedImageAA;
	import org.agony2d.notify.AEvent;
	import org.agony2d.timer.DelayManager;
	import org.agony2d.utils.formatString;

public class Benchmark_StateAA extends StateAA
{
	public function Benchmark_StateAA()
	{
	}
	
	/** overwritable... */
	override public function get smoothing() : Boolean {
		return true
	}
	
	/** overwritable... */
//	override public function get tinted() : Boolean {
//		return true
//	}
	
	/** overwrite... */
	override public function get fullTexture() : String {
		return "A_32"
	}
	
	private var mContainer:FusionAA;
	private var mFrameCount:int;
	private var mElapsed:Number;
	private var mStarted:Boolean;
	private var mFailCount:int;
	private var mWaitFrames:int;
	
	/** overwrite... */
	override public function enter() : void {
		// the container will hold all test objects
		mContainer = new FusionAA();
		mContainer.interactive = false; // we do not need touch events on the test objects -- 
		// thus, it is more efficient to disable them.
		this.group.addNode(mContainer);
		
		mStarted = false;
		mElapsed = 0.0;
		
		Agony.process.addEventListener(AEvent.ENTER_FRAME, onEnterFrame);
		this.onStart()
			
		mTxt = new TextField
		mTxt.text = "0"
		Agony.stage.addChild(mTxt)
		this.group.world.addEventListener(AEvent.PRESS, onToggle)
	}
	
	public override function exit():void
	{
		Agony.process.removeEventListener(AEvent.ENTER_FRAME, onEnterFrame);
	}
	
	
	private var mTxt:TextField

	
	private function onToggle(e:AEvent ) : void{
		mStarted = !mStarted
	}
	
	private function onEnterFrame(event:AEvent):void
	{
		if (!mStarted) return;
		
		mElapsed += Agony.process.elapsed * 0.001;
		mFrameCount++;
		
		if (mFrameCount % mWaitFrames == 0)
		{
			var fps:Number = mWaitFrames / mElapsed;
			var targetFps:int = Agony.stage.frameRate;
			
			if (Math.ceil(fps) >= targetFps)
			{
				mFailCount = 0;
				addTestObjects();
			}
			else
			{
				mFailCount++;
				
				if (mFailCount > 20)
					mWaitFrames = 5; // slow down creation process to be more exact
				if (mFailCount > 30)
					mWaitFrames = 10;
				if (mFailCount >= 40)
					benchmarkComplete(); // target fps not reached for a while
			}
			
			mElapsed = mFrameCount = 0;
		}
		
		var numObjects:int = mContainer.numNodes;
		var passedTime:Number = Agony.process.elapsed * 0.001;
		
		for (var i:int=0; i<numObjects; ++i)
			mList[i].rotation += 4
	}
	
	private var mList:Array = []
	
	private function onStart():void
	{
		trace("Starting benchmark");
		
		mStarted = true;
		mFailCount = 0;
		mWaitFrames = 2;
		mFrameCount = 0;
		
		//addTestObjects();
	}
	
	private function addTestObjects():void
	{
		var padding:int = 65;
		var numObjects:int = mFailCount > 20 ? 2 : 10;
		
		for (var i:int = 0; i<numObjects; ++i)
		{
//			var egg:TintedImageAA = new TintedImageAA
			var egg:ImageAA = new ImageAA
			mContainer.addNode(egg);
			mList.push(egg)
			egg.textureData = FULL_TEX;
			egg.pivotX = 16
			egg.pivotY = 16
//			egg.scaleX = egg.scaleY = 0.75
			//egg.scaleX = egg.scaleY = 1.25
			egg.x = padding + Math.random() * (Agony.fullWidth - 2 * padding);
			egg.y = padding + Math.random() * (Agony.fullHeight - 2 * padding);
//			egg.alpha = 0.1 + Math.random() * 0.9
//			egg.color = Math.random() * 0xFFFFFF
			
		}
		mTxt.text = mContainer.numNodes.toString()
	}
	
	private function benchmarkComplete():void
	{
		mStarted = false;

		var fps:int = Agony.stage.frameRate;
		
		trace("Benchmark complete!");
		trace("FPS: " + fps);
		trace("Number of objects: " + mContainer.numNodes);
		
		var text:TextField = new TextField
		Agony.stage.addChild(text)
		text.x = text.y = 300
		text.text = mContainer.numNodes.toString()
		
		var resultString:String = org.agony2d.utils.formatString("Result:\n{0} objects\nwith {1} fps",
			mContainer.numNodes, fps);

		mContainer.killAllNodes();
//		System.pauseForGCIfCollectionImminent();
	}
	
}
}